package com.magic.smartcity.view.me;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.BalanceBean;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.databinding.FragmentMeBalanceListBinding;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;
import com.magic.smartcity.view.adapters.MeBalanceListAdapter;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeBalanceListFragment extends BaseUserCallBackFragment {

    private static final String TAG = "MeBalanceListFragment";
    private FragmentMeBalanceListBinding mBinding;

    @Inject
    IUserPresenter mPresenter;
    private MeBalanceListAdapter mMeBalanceListAdapter;
    private String mToken;
    private AlertDialog.Builder mAlert;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentMeBalanceListBinding.inflate(getLayoutInflater());
        mPresenter.registerCallBack(this);
        setHasOptionsMenu(true);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {

        mMeBalanceListAdapter = new MeBalanceListAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mBinding.listRv.setLayoutManager(linearLayoutManager);
        mBinding.listRv.setHasFixedSize(true);
        mBinding.listRv.setAdapter(mMeBalanceListAdapter);


        if (Constant.TOKEN != null) {
            mPresenter.getBalanceList();
            mPresenter.getNowUser();
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    @Override
    public void balanceRechargeOk(String str) {
        mPresenter.getNowUser();
        mPresenter.getBalanceList();
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void balanceListLoaded(List<BalanceBean> beanList) {
        mMeBalanceListAdapter.setData(beanList);
    }

    @Override
    public void getUserLoaded(UserBean.UserBeanDo user) {
        mBinding.nowBalance.setText("当前金额: " + user.getBalance());
    }

    @Override
    public void error(String str) {

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.user_balance_recharge, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.user_balance_recharge) {
            //充值弹窗
            EditText editText = new EditText(getContext());
            editText.setBackground(getResources().getDrawable(R.color.touming));
            editText.setHint("请输入要充值的金额");
            editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            mAlert = new AlertDialog
                    .Builder(getContext())
                    .setTitle("充值金额")
                    .setView(editText)
                    .setPositiveButton("确认充值", (dialog, which) -> {
                        if (editText.getText().toString().trim().length()>0) {
                            int money = Integer.parseInt(editText.getText().toString());
                            if (money < 1) {
                                Toast.makeText(getContext(), "请输入正确的金额", Toast.LENGTH_SHORT).show();
                            } else if (money > 50000) {
                                Toast.makeText(getContext(), "充值金额不可大于5万", Toast.LENGTH_SHORT).show();
                            } else {
                                mPresenter.rechargeBalance(money);
                                dialog.dismiss();
                            }
                        }else{
                            Toast.makeText(getContext(), "请输入正确的金额", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("取消", (dialog, which) -> {
                        dialog.dismiss();
                    });

            mAlert.show();
        }
        return super.onOptionsItemSelected(item);
    }
}